home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / snip9611.zip / LIST.CPP < prev    next >
C/C++ Source or Header  |  1996-11-24  |  2KB  |  160 lines

  1. // +++Date last modified: 02-Nov-1995
  2.  
  3. /////////////////////////////////////////////////////////////
  4. // MODULE
  5. //  list.cpp
  6. // CREATED
  7. //  davidn  03 Dec 1994  23:59
  8. //  David L. Nugent
  9. //  This class implementation is donated to the public domain
  10. // DESCRIPTION
  11. //  Implementation of class node, list & iter
  12. // FUNCTIONS
  13. //  node::unlink()
  14. //    Destroys linkage from a list and removes it from a
  15. //    linked list
  16. //  node::link()
  17. //    Links a node into a linked list
  18. //  list::purge()
  19. //    Removes all linked list entries
  20. //  iter::traverse()
  21. //    Provides full traversal functions for nodes in a
  22. //    linked list
  23. /////////////////////////////////////////////////////////////
  24.  
  25. // Implementation of class list & friends
  26.  
  27. #include "list.hpp"
  28.  
  29. void
  30. node::unlink()
  31. {
  32.   if ( mylist )
  33.   {
  34.       // Unlink from previous
  35.     if ( Prev )
  36.       Prev->Next = Next;
  37.     else
  38.       mylist->First = Next;
  39.  
  40.       // Unlink from next
  41.     if ( Next )
  42.       Next->Prev = Prev;
  43.     else
  44.       mylist->Last = Prev;
  45.  
  46.     mylist->nodes--;
  47.     mylist = 0;
  48.     Prev = Next = 0;
  49.   }
  50. }
  51.  
  52. void
  53. node::link( list * L, node * prv, node * nxt )
  54. {
  55.  
  56.     // If currently linked, then unlink it
  57.  
  58.   if ( mylist )
  59.     unlink();
  60.  
  61.     // Link it to the list
  62.  
  63.   if ( L )
  64.   {
  65.  
  66.     mylist = L;
  67.  
  68.     // Add after previous
  69.  
  70.     if ( prv )
  71.     {
  72.  
  73.       Prev = prv;
  74.       Next = prv->Next;
  75.     }
  76.  
  77.     // Add before next
  78.  
  79.     else if ( nxt )
  80.     {
  81.  
  82.       Next = nxt;
  83.       Prev = nxt->Prev;
  84.     }
  85.  
  86.     // Else just add to end
  87.  
  88.     else
  89.     {
  90.  
  91.       Next = 0;
  92.       Prev = L->Last;
  93.     }
  94.  
  95.     if ( Prev )
  96.       Prev->Next = this;
  97.     else
  98.       L->First = this;
  99.  
  100.     if ( Next )
  101.       Next->Prev = this;
  102.     else
  103.       L->Last = this;
  104.  
  105.     mylist->nodes++;
  106.   }
  107. }
  108.  
  109.  
  110. void
  111. list::purge( void )
  112. {
  113.   while ( First )
  114.     delete First;
  115. }
  116.  
  117.  
  118. int
  119. iter::traverse( trOp op )
  120. {
  121.   if ( mylist.firstNode() == 0 )
  122.     return TR_EMPTY;
  123.  
  124.   int rc = TR_OK;
  125.  
  126.   switch ( op )
  127.   {
  128.  
  129.   case NEXT:
  130.     if ( nptr )
  131.     {
  132.  
  133.       nptr = nptr->Next;
  134.       break;
  135.     }
  136.  
  137.   case FIRST:
  138.     nptr = mylist.firstNode();
  139.     break;
  140.  
  141.  
  142.   case PREV:
  143.     if ( nptr )
  144.     {
  145.       nptr = nptr->Prev;
  146.       break;
  147.     }
  148.  
  149.   case LAST:
  150.     nptr = mylist.lastNode();
  151.     break;
  152.  
  153.   case CURR:
  154.     break;
  155.  
  156.   }
  157.  
  158.   return ( nptr ) ? TR_OK : TR_NOMORE;
  159. }
  160.